package se.grp03.fmtuner.scheduler.message;

import se.grp03.fmtuner.scheduler.io.UdpManager;


/**
 * MessageFactory handles messages by creating a specific Message type for each message.
 * @author Anthony Turner
 *
 */
public class MessageFactory {

	private static MessageFactory mf; // Single MessageFactory object
	
	//Private constructor
	private MessageFactory() {}
	
    /**
     * Precondition: A static call is made on this class and method
     * PostCondition: A single instance of a MessageFactory is returned
     * @return returns a single instance of a MessageFactory
     */
	public static MessageFactory getInstance() {            
		//If there is no instance of MessageFactory, then create one and only one instance.
		if( mf == null )
			mf = new MessageFactory();
		return mf;
	}

	/**
	 * Creates a message depending on the argument value type
	 * @param msg the income message to create a Message from.
	 * @return returns a message of a specific type
	 */
	public MessageInterface createMessage(String msg) {
		if(msg.length() > 0) {
			// SCAN message
			if(Character.toLowerCase(msg.charAt(0)) == 's')
				return new ScanMessage(msg);
			// TUNE message
			else if(Character.isDigit(msg.charAt(0)))
				return new TuneMessage(msg);
			// QUIT message (debug)
			else if(Character.toLowerCase(msg.charAt(0)) == 'q') {
				System.out.println("Terminating FM Tuner Scheduler...");
				UdpManager udp = UdpManager.getInstance();
				udp.stop();
				System.exit(0);
			}
		}
		return null;
	}
}